package com.lzy.huijinjiazu.mvp.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.lzy.huijinjiazu.R;


/**
 * Created by 欧源 on 2018/1/10.
 */

public class CircleProgress extends View {


    private Paint mTextPaint;

    private Paint mCirclePaint;

    private Paint mProgressPaint;

    private Context mContext;

    private float mRadius;

    private float mCircleWidth;

    private float mCenterPoint;

    private RectF mRectF;

    private float mPercent;

    private String mText;

    private float mBaseLineY;

    private float mCenterX;

    private int mCircleColor, mProgressColor, mTextColor;


    private int mTextSize;


    public CircleProgress(Context context) {
        this(context, null);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgress);

        try {

            mCircleColor = typedArray.getColor(R.styleable.CircleProgress_cp_circle_color, Color.parseColor("#CCCCCC"));
            mProgressColor = typedArray.getColor(R.styleable.CircleProgress_cp_progress_color, Color.parseColor("#FF00FF"));
            mTextColor = typedArray.getColor(R.styleable.CircleProgress_cp_text_color, Color.parseColor("#CCCCCC"));
            mTextSize = typedArray.getDimensionPixelSize(R.styleable.CircleProgress_cp_text_size, sp2px(22));
            mCircleWidth = typedArray.getDimensionPixelSize(R.styleable.CircleProgress_cp_circle_width, dp2px(5));
            mText = typedArray.getString(R.styleable.CircleProgress_cp_text);
            mPercent = typedArray.getFloat(R.styleable.CircleProgress_cp_progress_percent, 0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            typedArray.recycle();
        }
        init();
    }

    private void init() {
        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setStrokeCap(Paint.Cap.ROUND);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setStrokeWidth(mCircleWidth);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStrokeCap(Paint.Cap.ROUND);
        mCirclePaint.setStyle(Paint.Style.STROKE);

        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setStrokeWidth(mCircleWidth);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
        mProgressPaint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mRadius = (getMeasuredWidth() - mCircleWidth * 2) / 2;
        mCenterPoint = getMeasuredWidth() / 2;
        mRectF = new RectF(mCenterPoint - mRadius, mCenterPoint - mRadius, mCenterPoint + mRadius, mCenterPoint + mRadius);
        mCenterX = mRectF.centerX();
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mBaseLineY = mCenterX - fontMetrics.top / 2 - fontMetrics.bottom / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mCenterPoint, mCenterPoint, mRadius, mCirclePaint);
        canvas.drawArc(mRectF, -90, 360 * mPercent, false, mProgressPaint);
        canvas.drawText(mText, mCenterX, mBaseLineY, mTextPaint);
    }

    /**
     * 设置进度条百分比
     *
     * @param percent
     */
    public void setPercent(float percent) {
        mPercent = percent;
        invalidate();
    }

    /**
     * 设置中间文本内容
     *
     * @param text
     */
    public void setText(String text) {
        this.mText = text;
        invalidate();
    }


    private int dp2px(float dipValue) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    private int sp2px(float spValue) {
        final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public int px2sp(float pxValue) {
        final float fontScale = mContext.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
}
